#include <stdio.h>
#include <stdlib.h>
#include <stack.h>

stack_node_t *new_stack_node(int data)
{
    stack_node_t *node = (stack_node_t *)malloc(sizeof(stack_node_t));
    node->data = data;
    node->next = NULL;
    return node ;
}

int stack_push(stack_t *head, int data)
{
    stack_node_t *p = new_stack_node(data);
    if (*head == NULL)
    {
        *head = p;
        return 0;
    }

    p->next = *head;
    *head =  p;
    return 0;
}


stack_node_t *stack_pop(stack_t *head)
{
    if (*head == NULL)
        return NULL;
    stack_node_t *n = *head;
    *head = (*head)->next;
    return n;
}

int stack_empty(stack_t head)
{
    return head==NULL?1:0
}

void stack_destroy(stack_t *head)
{
    stack_node_t *t = NULL;
    while(*head != NULL)
    {
        t = *head;
        *head = (*head)->next;
        free(t);
    }
}

void stack_print(stack_t head)
{
    while(head != NULL)
    {
        printf("%d, ", head->data);
        head = head->next;
    }
    printf("\n");
}